<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use app\admin\model\Fin_account;//会计科目
use app\admin\model\Fin_co2buy;//收款登记
use app\admin\model\Journal;//日记账

class Finance extends Auth
{
    //会计科目列表
    public function finaccount(Request $request)
    {
        $res = $request->param();
        $res['lang'] = $this->lang;
        $finaccount = new Fin_account();
        if ($request->isAjax() && $request->isPost() && isset($res['getfinaccount'])) {
            $finaccounts = $finaccount->getfinaccount($res);
            $getfinaccount = array_slice($finaccounts['data'], ($res['page'] - 1) * $res['pageSize'], $res['pageSize']);
            echo json_encode(array('getfinaccount' => $getfinaccount, 'size' => $finaccounts['count']));
        } elseif ($request->isAjax() && $request->isPost() && isset($res['finaccounts'])) {
            //获取这个国家下的所有上级目录
            $data = $finaccount->finaccounts($res);
            echo json_encode($data);
        } elseif ($request->isGet()) {
            return view('finaccount/finaccount');
        }
    }

    //会计的添加
    public function finaccountadd(Request $request)
    {
        $data = $request->param();
        $finaccount = new Fin_account();
        if ($request->isPost() && $request->isAjax()) {
            $finaccountret = $finaccount->finaccountaddedit($data);
            $udata["status"] = $finaccountret['status'];
            $udata['msg'] = $finaccountret['msg'];
            echo json_encode($udata);
        }
    }

    //会计的编辑
    public function finaccountedit(Request $request)
    {
        $data = $request->param();
        $finaccount = new Fin_account();
        if ($request->isPost() && $request->isAjax() && isset($data['getfinaccount'])) {
            //编辑的请求接口
            echo json_encode(getinfo($data['id'], 'fin_account'));
        } elseif ($request->isAjax() && $request->isPost() && isset($data['act'])) {
            $finaccountret = $finaccount->finaccountaddedit($data);
            $udata["status"] = $finaccountret['status'];
            $udata['msg'] = $finaccountret['msg'];
            echo json_encode($udata);

        }
    }

    //会计科目的删除
    public function finaccountdel(Request $request)
    {
        $data = $request->param();
        $finaccount = new Fin_account();
        if ($request->isPost() && $request->isAjax()) {
            $finaccountret = $finaccount->finaccountdel($data);
            $udata["status"] = $finaccountret['status'];
            $udata['msg'] = $finaccountret['msg'];
            echo json_encode($udata);
        }
    }

    //会计科目设置权限
    public function setfinaccroot(Request $request)
    {
        $data = $request->param();
        $data['lang'] = $this->lang;
        $finaccount = new Fin_account();
        if ($request->isGet()) {
            return view('finaccount/setaccroot', [
                'id' => $data['id'],
                'str' => 'setfinaccroot'
            ]);
        } elseif ($request->isPost() && $request->isAjax() && isset($data['getroot'])) {
            $finaccountins = getpostion('fin_account', $data['id']);//当前会计的上一级id
            $name = implode('/', array_column(Db::name('fin_account')->field('name')->where('id', 'in', $finaccountins)->select(), 'name'));
            $admin_postion_id = session('admin')['position_id'];
            $country_id = '';
            if ($admin_postion_id != 1) {
                $country_id = getinfo($admin_postion_id, 'position')['country_id'];
            }
            $info = getinfo($data['id'], 'fin_account');
            echo json_encode(array('name' => $name, 'lang' => $data['lang'], 'info' => $info, 'country_id' => $country_id));
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'setaccroot') {
            $finaccountret = $finaccount->setaccroot($data);
            $udata["status"] = $finaccountret['status'];
            $udata['msg'] = $finaccountret['msg'];
            echo json_encode($udata);
        }
    }

    //日记账
    public function journal(Request $request)
    {
        $res = $request->param();
        $res['lang'] = $this->lang;
        $finaccount = new Fin_account();
        if ($request->isAjax() && $request->isPost() && isset($res['getjournal'])) {
            $journal = new Journal();
            $journals = $journal->getjournal($res);
            echo json_encode(array('getjournals' => $journals['data'], 'size' => $journals['count'], 'moneys' => $journals['moneys'], 'dateday' => $journals['dateday'], 'finaccounts' => $journals['finaccounts']));
        } elseif ($request->isAjax() && $request->isPost() && isset($res['finaccounts'])) {
            $datares = $finaccount->finaccounts($res);
            echo json_encode($datares);
        } elseif ($request->isGet()) {
            return view('journal/journal');
        }
    }

    //采购收支
    public function cotwobuy(Request $request)
    {
        $res = $request->param();
        $res['lang'] = $this->lang;
        $fin_co2buy = new Fin_co2buy();
        if ($request->isPost() && $request->isAjax() && isset($res['cotwobuy'])) {
            $fin_co2buys = $fin_co2buy->getfin_co2buys($res);
            echo json_encode($fin_co2buys);
        } elseif ($request->isPost() && $request->isAjax() && isset($res['getcompanyname'])) {
            //获取公司名称
            $company= new \app\admin\model\Company();
            $companys = $company->getcompanyname($res);
            echo json_encode($companys);
        } else if ($request->isPost() && $request->isAjax() && isset($res['getcompanyacc'])) {
            //获取公司的账号
            $co_bank = new \app\admin\model\Co_bank();
            $banks = $co_bank->getBanks($res);
            echo json_encode($banks);
        } else {
            return view('finaccount/cotwobuy');
        }
    }

    //付款登记的编辑
    public function cotwobuyedit(Request $request)
    {
        $data = $request->param();
        $fin_co2buy = new Fin_co2buy();
        if ($request->isAjax() && $request->isPost() && isset($data['getcotwobuyedit'])) {
            //编辑获取的接口
            $data['lang'] = $this->lang;
            $info = $fin_co2buy->cotwobuyedit($data);
            echo json_encode($info);
        } elseif ($request->isAjax() && $request->isPost() && $data['act'] == 'cotwobuyedit') {
            //编辑提交的接口
            $cotwobuyret = $fin_co2buy->cotwobuyaddedit($data);
            $udata["status"] = $cotwobuyret['status'];
            $udata['msg'] = $cotwobuyret['msg'];
            echo json_encode($udata);

        }
    }

    //付款登记的添加
    public function cotwobuyadd(Request $request)
    {
        $data = $request->param();
        $fin_co2buy = new Fin_co2buy();
        if ($request->isAjax() && $request->isPost() && $data['act'] == 'cotwobuyadd') {
            //添加提交的接口
            $cotwobuyret = $fin_co2buy->cotwobuyaddedit($data);
            $udata["status"] = $cotwobuyret['status'];
            $udata['msg'] = $cotwobuyret['msg'];
            echo json_encode($udata);
        }
    }

    //付款登记的的详情的点击
    public function cotwobuydetails(Request $request)
    {
        $data = $request->param();
        $data['lang'] = $this->lang;
        $fin_co2buy = new Fin_co2buy();
        if ($request->isGet()) {
            $datares = $fin_co2buy->cotwobuydetails($data);
            return view('finaccount/cotwodetails', [
                'data' => $datares,
            ]);
        }
    }

    //付款登记的删除
    public function cotwobuydel(Request $request)
    {
        $data = $request->param();
        $fin_co2buy = new Fin_co2buy();
        if ($request->isPost() && $request->isAjax()) {
            $cotwobuyret = $fin_co2buy->cotwobuydel($data);
            $udata["status"] = $cotwobuyret['status'];
            $udata['msg'] = $cotwobuyret['msg'];
            echo json_encode($udata);
        }
    }
}

